package com.getpebble.android.jskit.bridge;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.core.util.PblContentProviderUtil;
import com.getpebble.android.common.model.LockerApp;
import com.getpebble.android.framework.util.ByteUtils;
import com.getpebble.android.jskit.JsApplicationInfo;
import com.getpebble.android.jskit.JsInstalledApplicationInfo;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class InstalledAppsBridge {
    private static final String TAG = InstalledAppsBridge.class.getSimpleName();
    protected static InstalledAppsBridge sInstance;
    private final Context mContext;
    private Map<UUID, JsInstalledApplicationInfo> mInstalledApps = new HashMap();

    public static InstalledAppsBridge getInstance(Context c) {
        if (sInstance == null) {
            sInstance = new InstalledAppsBridge(c.getApplicationContext());
        }
        return sInstance;
    }

    protected InstalledAppsBridge(Context c) {
        this.mContext = c;
        scanForInstalledWebApps();
    }

    public String getDeveloperIdForApp(UUID app) {
        String[] selectionArgs = new String[]{app.toString()};
        Cursor cursor = this.mContext.getContentResolver().query(PblContentProviderUtil.getTableUri("locker_apps"), null, "uuid = ?", selectionArgs, null);
        if (cursor == null) {
            return null;
        }
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            ContentValues contentValues = new ContentValues();
            DatabaseUtils.cursorRowToContentValues(cursor, contentValues);
            LockerApp lockerApp = LockerApp.fromContentValues(contentValues);
            if (lockerApp != null) {
                cursor.close();
                return lockerApp.getDeveloperId();
            }
        }
        cursor.close();
        return null;
    }

    public String[] getCapabilitiesForApp(UUID app) {
        if (this.mInstalledApps == null || this.mInstalledApps.get(app) == null) {
            scanForInstalledWebApps();
        }
        JsApplicationInfo applicationInfo = (JsApplicationInfo) this.mInstalledApps.get(app);
        if (applicationInfo != null) {
            return applicationInfo.getCapabilities();
        }
        return null;
    }

    public Map<UUID, JsInstalledApplicationInfo> scanForInstalledWebApps() {
        File topLevelDir = null;
        try {
            topLevelDir = new File(JsAppInstaller.getInstance(this.mContext).getInstallDir().getAbsolutePath());
        } catch (Exception e) {
            Trace.debug(TAG, "scanForInstalledWebApps", e);
        }
        if (topLevelDir == null) {
            return new HashMap();
        }
        if (!topLevelDir.isDirectory()) {
            return new HashMap();
        }
        this.mInstalledApps = scanTopLevelDirectoryForWebApps(topLevelDir);
        return this.mInstalledApps;
    }

    protected Map<UUID, JsInstalledApplicationInfo> scanTopLevelDirectoryForWebApps(File topLevelDir) {
        Map<UUID, JsInstalledApplicationInfo> rv = new HashMap();
        File[] topLevelList = topLevelDir.listFiles();
        if (topLevelList != null && topLevelList.length > 0) {
            for (File possibleAppDir : topLevelList) {
                if (possibleAppDir.isDirectory()) {
                    try {
                        JsInstalledApplicationInfo appInfo = scanAppDirectory(possibleAppDir);
                        if (appInfo != null) {
                            rv.put(appInfo.getUuid(), appInfo);
                        }
                    } catch (Exception e) {
                        Trace.debug(TAG, "scanTopLevelDirectoryForWebApps", e);
                    }
                }
            }
        }
        return rv;
    }

    public JsInstalledApplicationInfo scanAppDirectory(File appDir) {
        JsApplicationInfo appInfoObject = tryAndMakeAppInfoForAppInAppDirectory(appDir);
        if (appInfoObject == null || appInfoObject.getUuid() == null) {
            return null;
        }
        JsInstalledApplicationInfo installedAppInfo = new JsInstalledApplicationInfo(appDir.getAbsolutePath(), appInfoObject);
        installedAppInfo.setJsCodeDigest(ByteUtils.MD5DigestStringFromInputFQPN((installedAppInfo.getInstalledAbsPathOnFilesystem().startsWith("/") ? "" : "/") + installedAppInfo.getInstalledAbsPathOnFilesystem() + "/pebble-js-app.js"));
        installedAppInfo.setUpdatedScannedTime(System.currentTimeMillis());
        return installedAppInfo;
    }

    public JsApplicationInfo tryAndMakeAppInfoForAppInAppDirectory(File appDir) {
        InputStream fileInForDigest;
        IOException e;
        Exception e2;
        InputStream fileIn;
        InputStream fileIn2;
        JsonElement obj;
        Gson g;
        Throwable th;
        if (appDir == null) {
            Trace.debug(TAG, "tryAndMakeAppInfoForAppInAppDirectory: appDir is null");
            return null;
        }
        File[] appDirList = appDir.listFiles();
        if (appDirList != null && appDirList.length > 0) {
            for (File possibleAppInfo : appDirList) {
                if (!possibleAppInfo.isDirectory() && possibleAppInfo.getName().compareToIgnoreCase("appinfo.json") == 0) {
                    JsApplicationInfo rv;
                    String digest = null;
                    fileInForDigest = null;
                    try {
                        InputStream fileInForDigest2 = new FileInputStream(possibleAppInfo);
                        try {
                            digest = ByteUtils.MD5DigestStringFromInputStream(fileInForDigest2);
                            if (fileInForDigest2 != null) {
                                try {
                                    fileInForDigest2.close();
                                    fileInForDigest = fileInForDigest2;
                                } catch (IOException e3) {
                                    Trace.debug(TAG, "error closing file", e3);
                                    fileInForDigest = fileInForDigest2;
                                }
                            }
                        } catch (Exception e4) {
                            e2 = e4;
                            fileInForDigest = fileInForDigest2;
                            try {
                                Trace.debug(TAG, "tryAndMakeAppInfoForAppInAppDirectory", e2);
                                if (fileInForDigest != null) {
                                    try {
                                        fileInForDigest.close();
                                    } catch (IOException e32) {
                                        Trace.debug(TAG, "error closing file", e32);
                                    }
                                }
                                fileIn = null;
                                fileIn2 = new FileInputStream(possibleAppInfo);
                                try {
                                    obj = new JsonParser().parse(new BufferedReader(new InputStreamReader(fileIn2))).getAsJsonObject();
                                    g = new Gson();
                                    rv = (JsApplicationInfo) g.fromJson(obj.get("info"), JsApplicationInfo.class);
                                    if (rv != null) {
                                        rv = (JsApplicationInfo) g.fromJson(obj, JsApplicationInfo.class);
                                        if (rv != null) {
                                            if (fileIn2 != null) {
                                                continue;
                                            } else {
                                                try {
                                                    fileIn2.close();
                                                } catch (IOException e322) {
                                                    try {
                                                        Trace.debug(TAG, "error closing file", e322);
                                                    } catch (Exception e22) {
                                                        Trace.debug(TAG, "tryAndMakeAppInfoForAppInAppDirectory", e22);
                                                    }
                                                }
                                            }
                                        } else {
                                            rv.setAppInfoDigest(digest);
                                            if (fileIn2 != null) {
                                                return rv;
                                            }
                                            try {
                                                fileIn2.close();
                                                return rv;
                                            } catch (IOException e3222) {
                                                Trace.debug(TAG, "error closing file", e3222);
                                                return rv;
                                            }
                                        }
                                    }
                                    rv.setAppInfoDigest(digest);
                                    if (fileIn2 != null) {
                                        return rv;
                                    }
                                    try {
                                        fileIn2.close();
                                        return rv;
                                    } catch (IOException e32222) {
                                        Trace.debug(TAG, "error closing file", e32222);
                                        return rv;
                                    }
                                } catch (IOException e5) {
                                    e32222 = e5;
                                    fileIn = fileIn2;
                                    Trace.debug(TAG, "tryAndMakeAppInfoForAppInAppDirectory", e32222);
                                    if (fileIn == null) {
                                        try {
                                            fileIn.close();
                                        } catch (IOException e322222) {
                                            Trace.debug(TAG, "error closing file", e322222);
                                        }
                                    } else {
                                        continue;
                                    }
                                } catch (Exception e6) {
                                    e22 = e6;
                                    fileIn = fileIn2;
                                    try {
                                        Trace.debug(TAG, "tryAndMakeAppInfoForAppInAppDirectory", e22);
                                        if (fileIn == null) {
                                            try {
                                                fileIn.close();
                                            } catch (IOException e3222222) {
                                                Trace.debug(TAG, "error closing file", e3222222);
                                            }
                                        } else {
                                            continue;
                                        }
                                    } catch (Throwable th2) {
                                        th = th2;
                                    }
                                } catch (Throwable th3) {
                                    th = th3;
                                    fileIn = fileIn2;
                                }
                            } catch (Throwable th4) {
                                th = th4;
                            }
                        } catch (Throwable th5) {
                            th = th5;
                            fileInForDigest = fileInForDigest2;
                        }
                    } catch (Exception e7) {
                        e22 = e7;
                        Trace.debug(TAG, "tryAndMakeAppInfoForAppInAppDirectory", e22);
                        if (fileInForDigest != null) {
                            fileInForDigest.close();
                        }
                        fileIn = null;
                        fileIn2 = new FileInputStream(possibleAppInfo);
                        obj = new JsonParser().parse(new BufferedReader(new InputStreamReader(fileIn2))).getAsJsonObject();
                        g = new Gson();
                        rv = (JsApplicationInfo) g.fromJson(obj.get("info"), JsApplicationInfo.class);
                        if (rv != null) {
                            rv.setAppInfoDigest(digest);
                            if (fileIn2 != null) {
                                return rv;
                            }
                            fileIn2.close();
                            return rv;
                        }
                        rv = (JsApplicationInfo) g.fromJson(obj, JsApplicationInfo.class);
                        if (rv != null) {
                            rv.setAppInfoDigest(digest);
                            if (fileIn2 != null) {
                                return rv;
                            }
                            fileIn2.close();
                            return rv;
                        }
                        if (fileIn2 != null) {
                            fileIn2.close();
                        } else {
                            continue;
                        }
                    }
                    fileIn = null;
                    try {
                        fileIn2 = new FileInputStream(possibleAppInfo);
                        obj = new JsonParser().parse(new BufferedReader(new InputStreamReader(fileIn2))).getAsJsonObject();
                        g = new Gson();
                        rv = (JsApplicationInfo) g.fromJson(obj.get("info"), JsApplicationInfo.class);
                        if (rv != null) {
                            rv.setAppInfoDigest(digest);
                            if (fileIn2 != null) {
                                return rv;
                            }
                            fileIn2.close();
                            return rv;
                        }
                        rv = (JsApplicationInfo) g.fromJson(obj, JsApplicationInfo.class);
                        if (rv != null) {
                            rv.setAppInfoDigest(digest);
                            if (fileIn2 != null) {
                                return rv;
                            }
                            fileIn2.close();
                            return rv;
                        } else if (fileIn2 != null) {
                            fileIn2.close();
                        } else {
                            continue;
                        }
                    } catch (IOException e8) {
                        e3222222 = e8;
                        Trace.debug(TAG, "tryAndMakeAppInfoForAppInAppDirectory", e3222222);
                        if (fileIn == null) {
                            continue;
                        } else {
                            fileIn.close();
                        }
                    } catch (Exception e9) {
                        e22 = e9;
                        Trace.debug(TAG, "tryAndMakeAppInfoForAppInAppDirectory", e22);
                        if (fileIn == null) {
                            continue;
                        } else {
                            fileIn.close();
                        }
                    }
                }
            }
        }
        return null;
        throw th;
        if (fileIn != null) {
            try {
                fileIn.close();
            } catch (IOException e32222222) {
                Trace.debug(TAG, "error closing file", e32222222);
            }
        }
        throw th;
        throw th;
        if (fileInForDigest != null) {
            try {
                fileInForDigest.close();
            } catch (IOException e322222222) {
                Trace.debug(TAG, "error closing file", e322222222);
            }
        }
        throw th;
    }
}
